// pages/home/pages/party5Apply/party5Apply.js
var r = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    message:'',

    id:'',
    date:'',

    time: '', //预约时间
    index: 0,
    multiArray: [
      [],
      ['06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00'],
      ['07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
    ], //两个时间段
    multiIndex: [0,0, 0], //默认值
  },

  // 选择时间区间
  //时间段选择
  bindMultiPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(e.detail.value[0]); //获取第一个列改变的值
    console.log(e.detail.value[1]); //获取第二个列改变的值.
    console.log(e.detail.value[2]); //获取第三个列改变的值.
    if (e.detail.value[1] <= e.detail.value[2]) { //如果第二列的值小于等于第三列的值
      that.setData({
        multiIndex: e.detail.value, //输出时间段
      })
      console.log(that.data.multiArray[0][that.data.multiIndex[0]]+' '+that.data.multiArray[1][that.data.multiIndex[1]]+'-'+that.data.multiArray[2][that.data.multiIndex[2]])
      let time = that.data.multiArray[0][that.data.multiIndex[0]]+' '+that.data.multiArray[1][that.data.multiIndex[1]]+'-'+that.data.multiArray[2][that.data.multiIndex[2]]
      that.setData({
        time:time
      })
    } else { //否则提示
      that.setData({
        time:''
      })
      wx.showToast({
        title: '请选择正确的时间段！',
        mask: true,
        icon: 'none',
      })
    }
  },

  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    // 知道修改的列以后，就可以通过修改multiIndex中对应元素的值，然后再修改multiArray
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      date:options.time
    })

    // 获取年月日，放入数组第一列
    let multiArray = this.data.multiArray
    multiArray[0] = [options.time]
    this.setData({
      multiArray: multiArray
    })
  },

  // 获取值
  getval(e) {
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      this.setData({
        phone: e.detail.value
      })
    } else if (type == 2) {
      this.setData({
        message: e.detail.value
      })
    }
  },

  // 申请
  submit(){
    let myreg = /^1[3456789]\d{9}$/;
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确手机号码'
      });
      return
    }

    if (!this.data.message) {
      wx.showToast({
        icon: 'none',
        title: '请输入申请原因'
      });
      return
    }

    if (!this.data.time) {
      wx.showToast({
        icon: 'none',
        title: '请选择时间段'
      });
      return
    }
    // console.log(this.data.id)
    // console.log(this.data.phone)
    // console.log(this.data.message)
    // console.log(this.data.time)
    // console.log(this.data.time.split(' ')[0])
    // console.log(this.data.time.split(' ')[1])
    let param = {
      roomId:this.data.id,
      telephone:this.data.phone,
      useReason:this.data.message,
      userId:wx.getStorageSync('userId'),
      useDateStr:this.data.time.split(' ')[0],
      usetime:this.data.time.split(' ')[1]
    }
    r.toPost('/activityroomApply/roomApply', param, (res) => {
      wx.showToast({
        title: '操作成功',
        icon:'none'
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 2,
        })
      },1000)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})